﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WaterRevenueMS.WebUI.Extension.Filters;
using WaterRevenueMS.Domain.ViewModels.BaseData.Company;
using WaterRevenueMS.Core.Service.BaseData;
using System.ComponentModel.Composition;
using WaterRevenueMS.Domain.Common.Models;
using WaterRevenueMS.WebUI.Common;
using System.Linq.Expressions;
using WaterRevenueMS.Componnent.Entities.BasicSet;
using WaterRevenueMS.WebUI.Extension.MvcHelper;
using WaterRevenueMS.Componnent.Common;

namespace WaterRevenueMS.WebUI.Controllers
{
    [Export]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class CompanyController : AdminController
    {
        [Import]
        public ICompanyService CompanyService { get; set; }

        [AdminLayoutAttribute]
        public ActionResult Index()
        {
           
            var model = new CompanyModel();
          
            
            
            return View(model);
             
        }

        [AdminPermission(PermissionCustomMode.Ignore)]
        public ActionResult List(DataTableParameter param)
        {
            int total = CompanyService.Companys.Count(t=>t.IsDeleted==false);

            var expr = BuildSearchCriteria();

            var filterResult = CompanyService.Companys.Where(expr).Select(t => new CompanyModel
            {
               
            }).OrderBy(t => t.Id).Skip(param.iDisplayStart).Take(param.iDisplayLength).ToList();

            int sortId = param.iDisplayStart + 1;

            var result = from c in filterResult
                         select new[]
                        {
                            sortId++.ToString(), 
                            c.Name,
                            c.ParentName,  
                            c.OrderNo.ToString(),
                            c.Id.ToString()
                        };

            return Json(new
            {
                sEcho = param.sEcho,
                iDisplayStart = param.iDisplayStart,
                iTotalRecords = total,
                iTotalDisplayRecords = total,
                aaData = result
            }, JsonRequestBehavior.AllowGet);
        }


        public ActionResult Create()
        {
            var model = new CompanyModel();
            InitParentCompany(model);
            return PartialView(model);
        }

        [HttpPost]
        [AdminOperateLog]
        public ActionResult Create(CompanyModel model)
        {
            if (ModelState.IsValid)
            {
                OperationResult result = CompanyService.Insert(model);
                if (result.ResultType == OperationResultType.Success)
                {
                    return Json(result);
                }
                else
                {
                    return PartialView(model);
                }
            }
            else
            {
                return PartialView(model);
            }
        }

        public ActionResult Edit(int Id)
        {
            var model = new CompanyModel();
            var entity=CompanyService.Companys.FirstOrDefault(t => t.Id == Id);
            if (null != entity)
            {
                model = new CompanyModel
                {

                    
                };
                InitParentCompany(model);
            }
            return PartialView(model);
        }

        [HttpPost]
        public ActionResult Edit(CompanyModel model)
        {
            if (ModelState.IsValid)
            {
                OperationResult result=CompanyService.Update(model);
                if (result.ResultType == OperationResultType.Success)
                {
                    return Json(result);
                }
                else
                {
                    return PartialView(model);
                }

            }
            else
            {
                return PartialView(model);
            }
        }


        public ActionResult Delete(int Id)
        {
            OperationResult result=CompanyService.Delete(Id);
            return Json(result);
        }

        public void InitParentCompany(CompanyModel model)
        {
            var parentCompanyData = CompanyService.Companys.Where(t => t.Gong_SBZ == null)
                .Select(t => new CompanyModel
                {
                    
                });
            foreach (var item in parentCompanyData)
            {
                model.ParentCompanyItems.Add(new SelectListItem { Text = item.Name, Value = item.Id.ToString() });
            }
        }

        private Expression<Func<CompanySet, Boolean>> BuildSearchCriteria()
        {
            DynamicLambda<CompanySet> bulider = new DynamicLambda<CompanySet>();
            Expression<Func<CompanySet, Boolean>> expr = null;
            if(!string.IsNullOrEmpty(Request["Name"]))
            {
                
            }
            if (!string.IsNullOrEmpty(Request["ParentId"]) && Request["ParentId"] != "0")
            {
                
            }

            Expression<Func<CompanySet, Boolean>> tmpSolid = t => t.IsDeleted == false;
            expr = bulider.BuildQueryAnd(expr, tmpSolid);

            return expr;
        }

    }
}
